package com.gzedu.common.sys.dao.impl;

import java.lang.reflect.ParameterizedType;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.stereotype.Repository;

import com.gzedu.common.sys.dao.IBaseDao;
import com.gzedu.common.sys.model.BaseEntity;
import com.gzedu.common.util.page.Page;
@Repository
public class BaseDao<T extends BaseEntity> extends SqlSessionDaoSupport implements IBaseDao<T>{


	@Resource
    public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
        super.setSqlSessionTemplate(sqlSessionTemplate);
    }


	@SuppressWarnings("unchecked")
    protected final String getStatement() {
        Class<T> clazz = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass())
                .getActualTypeArguments()[0];
 
        return "com.gzedu.module.dao." + clazz.getSimpleName()+"Dao";
    }



		
		public int insert(T e) {
			// TODO Auto-generated method stub
			return this.getSqlSession().insert(getStatement()+".insert",e);
		}
		
		public long replace(T e) {
			// TODO Auto-generated method stub
			return this.getSqlSession().insert(getStatement()+".replace",e);
		}
		
		public int update(T e) {
			// TODO Auto-generated method stub
			return this.getSqlSession().update(getStatement()+".update");
		}



		
		public List<T> findList(T entity) {
			// TODO Auto-generated method stub
			return this.getSqlSession().selectList(getStatement()+".findList",entity);
		}



		
		public Page<T> page(T entity, int page, int pagesize) {
			// TODO Auto-generated method stub
			if (page < 1) {
	            throw new IllegalArgumentException("Page argument value must be positive integer");
	        }
	 
	        if (pagesize < 1) {
	            throw new IllegalArgumentException("Pagesize argument value must be positive integer");
	        }
	        
	        
	        
	        int totalRecords = count(entity);
	 
	        Page<T> pagination = new Page<T>();
	        pagination.setPageNo(page);
	        pagination.setPageSize(pagesize);
	        List<T> result = this.getSqlSession().selectList(getStatement()+".findPage", pagination);
	        pagination.setResults(result);
	        return pagination;
		}



		
		public T getById(String id) {
			// TODO Auto-generated method stub
			return this.getSqlSession().selectOne(getStatement()+".getById", id);
		}



		
		public List<T> getAll() {
			// TODO Auto-generated method stub
			return this.getSqlSession().selectList(getStatement()+".getAll");
		}

		
		public int deleteByPrimary(T entity) {
			// TODO Auto-generated method stub
			return this.getSqlSession().delete(getStatement()+".deleteById", entity);
		}



		
		public int clear() {
			// TODO Auto-generated method stub
			return 0;
		}


		public int updateById(T e) {
			// TODO Auto-generated method stub
			return this.getSqlSession().update(getStatement() + ".update", e);
		}


		public T getOne(T entity) {
			// TODO Auto-generated method stub
			return this.getSqlSession().selectOne(getStatement()+".getById", entity);
		}


		public List<T> select(T entity) {
			// TODO Auto-generated method stub
			return null;
		}


		public int count(T entity) {
			// TODO Auto-generated method stub
			return 0;
		}


		public int delete(T entity) {
			// TODO Auto-generated method stub
			return 0;
		}


		public int deleteByPrimary(String primary) {
			// TODO Auto-generated method stub
			return 0;
		}


		public List<T> findAllList() {
			// TODO Auto-generated method stub
			return this.getSqlSession().selectList(getStatement()+".findAllList");
		}

	 

}
